package com.xbongbong.paas.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.thread.TransmittableThreadLocalUtil;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.detailtab.dto.LinkAddBeforeDTO;
import com.xbongbong.pro.detailtab.vo.DetailTabLinkAddBeforeVO;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.paymenttask.pojo.dto.PaymentTaskGetDTO;
import com.xbongbong.pro.paymenttask.pojo.vo.PaymentTaskPlanDayListVO;
import com.xbongbong.pro.paymenttask.pojo.vo.PaymentTaskPlanVO;
import com.xbongbong.pro.paymenttask.pojo.vo.PaymentTaskStatisicsListVO;
import com.xbongbong.pro.paymenttask.pojo.vo.PaymentTaskVO;
import com.xbongbong.pro.weblist.pojo.dto.SaasBaseDTO;
import com.xbongbong.pro.weblist.pojo.vo.SpecialListFilterVO;
import com.xbongbong.saas.service.PaymentTaskService;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * @author: 汪腾伟
 * @date: 2020/7/29
 * @desrcption: 回款计划
 * @version:1.0
 */
@RestController
@RequestMapping(XbbProConstant.API_ROOT_PREFIX + "/paymentTask")
public class PaymentTaskController {

    @Resource
    private PaymentTaskService paymentTaskService;

    /**
     * 回款计划获取查询字段的接口
     *
     * @param saasBaseDTO
     * @param br
     * @return
     */
    @RequestMapping(value = "/filter/specialList", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String statusSearch(@RequestBody @Valid SaasBaseDTO saasBaseDTO, BindingResult br) {
        XbbResponse<SpecialListFilterVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                SpecialListFilterVO specialListFilterVO = paymentTaskService.statusSearch(saasBaseDTO);
                response = new XbbResponse<>(specialListFilterVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }

        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * 获取回款计划的金额信息
     *
     * @param paymentTaskGetDTO
     * @param br
     * @return
     */
    @RequestMapping(value = "/paymentTaskAmount/get", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getPaymentTaskAmount(@RequestBody @Valid PaymentTaskGetDTO paymentTaskGetDTO, BindingResult br) {
        XbbResponse<PaymentTaskVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                PaymentTaskVO paymentTaskVO = paymentTaskService.getPaymentTaskAmount(paymentTaskGetDTO);
                response = new XbbResponse<>(paymentTaskVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }


    /**
     * 获取回款计划列表
     * @param paymentTaskGetDTO 接受查询参数用的回款计划实体
     * @param br
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String listPaymentTask(@RequestBody @Valid PaymentTaskGetDTO paymentTaskGetDTO, BindingResult br) {
        XbbResponse<PaymentTaskPlanVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                TransmittableThreadLocalUtil.setValue(paymentTaskGetDTO.getSaasMark(), paymentTaskGetDTO.getBusinessType(), null, paymentTaskGetDTO.getDistributorMark());
                PaymentTaskPlanVO paymentTaskVO = paymentTaskService.listPaymentTask(paymentTaskGetDTO);
                response = new XbbResponse<>(paymentTaskVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     *获取哪些天有回款计划
     *
     *
     * @param paymentTaskGetDTO
     * @param br
     * @return
     */
    @RequestMapping(value = "/dayList", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String dayList(@RequestBody @Valid PaymentTaskGetDTO paymentTaskGetDTO, BindingResult br) {
        XbbResponse<PaymentTaskPlanDayListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                TransmittableThreadLocalUtil.setValue(paymentTaskGetDTO.getSaasMark(), paymentTaskGetDTO.getBusinessType(), null, paymentTaskGetDTO.getDistributorMark());
                PaymentTaskPlanDayListVO paymentTaskPlanDayListVO = paymentTaskService.dayList(paymentTaskGetDTO);
                response = new XbbResponse<>(paymentTaskPlanDayListVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * 当前日期回款计划统计
     * @param paymentTaskGetDTO
     * @param br
     * @return
     */
    @RequestMapping(value = "/statistics", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String statistics(@RequestBody @Valid PaymentTaskGetDTO paymentTaskGetDTO, BindingResult br) {
        XbbResponse<PaymentTaskStatisicsListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                PaymentTaskStatisicsListVO paymentTaskStatisicsListVO = paymentTaskService.statistics(paymentTaskGetDTO);
                response = new XbbResponse<>(paymentTaskStatisicsListVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }

        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }



    /**
     * 直接完成回款计划
     * @param linkAddBeforeDTO
     * @param br
     * @return
     */
    @RequestMapping(value = "/updatePaymentTaskStatus", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String updatePaymentTaskStatus(@RequestBody @Valid LinkAddBeforeDTO linkAddBeforeDTO, BindingResult br) {
        XbbResponse<DetailTabLinkAddBeforeVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                DetailTabLinkAddBeforeVO detailTabLinkAddBeforeVO = paymentTaskService.updatePaymentTaskStatus(linkAddBeforeDTO);
                response = new XbbResponse<>(detailTabLinkAddBeforeVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }

        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }


}
